package com.zzw.filters.xssfilter;

import com.zzw.constant.OrderConstant;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @author Administrator
 */
@Configuration
@ConditionalOnProperty(prefix = "commons.server.xss-filter", name = "enable")
public class XssFilterConfiguration {

    @Bean
    @ConditionalOnMissingBean
    XssHandler getXssHandler() {
        return new DefaultXssHandler();
    }

    @Bean
    FilterRegistrationBean<XssFilter> getXssFilterRegistrationBean(XssHandler xssHandler) {
        FilterRegistrationBean<XssFilter> filterRegistrationBean = new FilterRegistrationBean<>();
        filterRegistrationBean.setFilter(new XssFilter(xssHandler));
        filterRegistrationBean.setOrder(OrderConstant.XSS_FILTER_ORDER);
        filterRegistrationBean.setEnabled(true);
        filterRegistrationBean.addUrlPatterns("/*");
        return filterRegistrationBean;
    }
}
